﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ANZ_AIM_DAL;
using ANZ_AIM_BLL.Model;
using ANZ_AIM_BLL.Utils;
using ANZ_AIM_FRAMEWORK;
using System.Data;
using System.Data.OleDb;

namespace ANZ_AIM_BLL
{
    public class ConfigurationBLL
    {
        private string ConnectionString;
        private const string TABLE_NAME = "Configurations";
        private Dictionary<string, string> ObjectPropDict;

        public ConfigurationBLL()
        {
            ConnectionString = DBHelper.GetMainMdbConnectionString();
            this.ObjectPropDict = BuildObjectPropertyMapping();
        }

        public Configuration GetByKey(string configId)
        {
            try
            {
                var dict = MiscHelper.Clone(this.ObjectPropDict);
                OleDbParameter[] parameters;
                Configuration config = new Configuration { ConfigID = configId };
                var conditionDict = new Dictionary<string, string>
                    {
                        {"ConfigID", "ConfigID"}
                    };
                string command = CommandHelper.BuildSelectCommand(config, dict, conditionDict, out parameters);
                OleDbDataReader reader = OleDbHelper.ExecuteReader(this.ConnectionString, CommandType.Text, command, parameters);
                
                List<Configuration> configs = new List<Configuration>();
                while (reader.Read())
                {
                    configs.Add(PopulateObjectHelper.PopulateConfigurationObject(reader));
                }
                reader.Close();
                return (configs.Count > 0 ? configs[0] : null);
            }
            catch (Exception ex) { throw; }
        }

        public void UpdateConfiguration(List<Configuration> configurationList)
        {
            try
            {
                var dict = MiscHelper.Clone(this.ObjectPropDict);
                dict.Remove("ConfigID");
                dict.Remove("Description");
                OleDbParameter[] parameters;
                string command = string.Empty;
                foreach (Configuration configuration in configurationList)
                {
                    var conditionDict = new Dictionary<string, string>
                    {
                        {"ConfigID", "ConfigID"}
                    };
                    command = CommandHelper.BuildUpdateCommand(configuration, dict, conditionDict, out parameters);
                    int n = OleDbHelper.ExecuteNonQuery(this.ConnectionString, CommandType.Text, command, parameters);
                }
            }
            catch (Exception ex) { throw; }
        }

        public Dictionary<string, string> BuildObjectPropertyMapping()
        {
            Dictionary<string, string> dictionary = new Dictionary<string, string>
            {
                {"ConfigID", "ConfigID"},
                {"ConfigValue", "ConfigValue"},
                {"Description", "Description"},
            };

            return dictionary;
        }
    }
}
